﻿using System;
using System.ComponentModel;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Events;
using UserManager.Module.Services;
using AuthenticationService.Client;
using AuthenticationService.Web;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Ria.ApplicationServices;

namespace UserManager.Module.ViewModels
{
    /// <summary>
    /// UserManagerViewModel for UserManagerView.
    /// </summary>
    public class UserManagerViewModel : INotifyPropertyChanged
    {
        WebContext _context;
        AuthenticationContext _authenticationContext;
        private readonly IEventAggregator _eventAggregator;
        ObservableCollection<User> _users;
        User _currentUser;

        public string HeaderInfo
        {
            get
            {
                return "User Manager";
            }
        }

        public User CurrentUser
        {
            get 
            { 
                return _currentUser; 
            }
            set 
            { 
                _currentUser = value;
                NotifyPropertyChanged("CurrentUser");
            }
        }

        public ObservableCollection<User> Users
        {
            get { return _users; }
        }



        public UserManagerViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _context = WebContext.Current;
            _context.Authentication.LoggedIn += new EventHandler<AuthenticationEventArgs>(Authentication_LoggedIn);
            _context.Authentication.LoggedOut += new EventHandler<AuthenticationEventArgs>(Authentication_LoggedOut);
            _authenticationContext = ((WebAuthenticationService)WebContext.Current.Authentication).DomainContext as AuthenticationContext;
            _users = new ObservableCollection<User>();
            _users.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_users_CollectionChanged);
            GetAllUsers();
        }

        void Authentication_LoggedOut(object sender, AuthenticationEventArgs e)
        {
            GetAllUsers();
        }

        void Authentication_LoggedIn(object sender, AuthenticationEventArgs e)
        {
            GetAllUsers();
        }

        void _users_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            
        }

        private void GetAllUsers()
        {
            _users.Clear();
            if (_context.User.IsAuthenticated)
            {
                _authenticationContext.Load(_authenticationContext.GetAllUsersQuery(), (result) =>
                    {
                        if (!result.HasError)
                        {
                            foreach (User user in result.AllEntities)
                            {
                                _users.Add(user);
                            }
                        }
                    }, true);
            }
        }

        #region Commands

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
